package com.sun.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.entity.Drug;
import com.sun.entity.Msg;
import com.sun.entity.Store;
import com.sun.entity.User;
import com.sun.service.DrugService;
import com.sun.service.MsgService;
import com.sun.service.StoreService;
import com.sun.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/3/31 15:21
 * @FileName: InventoryFloorWarning
 * @Description: ""
 */
public class InventoryFloorWarning extends QuartzJobBean {
    @Autowired
    private DrugService drugService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private UserService userService;
    @Autowired
    private MsgService msgService;

    @Override
    protected void executeInternal(@NotNull JobExecutionContext context) throws JobExecutionException {
        //TODO 这里写定时任务的执行逻辑
        //查询出药品库存小于下限的
        LocalDateTime now = LocalDateTime.now();
        Map<String, String> result = new HashMap<>();
        LambdaQueryWrapper<Drug> drugLambdaQueryWrapper = new LambdaQueryWrapper<>();
        drugLambdaQueryWrapper.isNotNull(Drug::getLowerlimit);
        List<Drug> drugList = drugService.list(drugLambdaQueryWrapper);
        for (Drug drug : drugList) {
            Store store = new Store();
            LambdaQueryWrapper<Store> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Store::getDrugId, drug.getDrugId());
            List<Store> stores = storeService.list(lambdaQueryWrapper);
            if (stores.isEmpty()) {
                String str = String.format("%s日,%s没有入库记录，库房没有该药品", now.toString().substring(0, 10), drug.getDrugName());
                result.put(drug.getDrugName(), str);
            } else {
                int num = 0;
                for (int i = 0; i < stores.size(); i++) {
                    num = num + stores.get(i).getDrugNumber();
                }
                if (num < drug.getLowerlimit()) {
                    String str = String.format("%s日,%s库存不足", now.toString().substring(0, 10), drug.getDrugName());
                    result.put(drug.getDrugName(), str);
                }
            }
        }
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getType, 2);
        userLambdaQueryWrapper.eq(User::getStatus, 1);
        List<User> users = userService.list(userLambdaQueryWrapper);
        sendMsg(result, now, "库存下限报警", users);
    }

    public void sendMsg(Map<String, String> list, LocalDateTime now, String type, List<User> users) {
        if (!list.isEmpty()) {
            for (Map.Entry<String, String> item : list.entrySet()) {
                for (User user : users) {
                    Msg msg = new Msg();
                    msg.setTitle(type);
                    msg.setContent(item.getValue());
                    msg.setDrugName(item.getKey());
                    msg.setDateTime(now);
                    msg.setStatus(0);
                    msg.setMsgUserId(1L);
                    msg.setUserId(user.getId());
                    msgService.save(msg);
                }
            }
        }
    }
}
